<template>
	<div class="system-menu-container layout-pd">
		<el-card shadow="hover">
			<div class="system-menu-search mb15">
				<el-button size="default" type="success" @click="onOpenAddMenu(1)">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增菜单
				</el-button>
			</div>
			<el-table
				:data="state.tableData.data"
				v-loading="state.tableData.loading"
				style="width: 100%"
				row-key="id"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>
				<el-table-column label="配置名称" min-width="150">
					<template #default="scope">
						<template v-if="scope.row.type == 2">
							{{ scope.row.name }}
						</template>
						<template v-else>
							<SvgIcon :name="scope.row.icon" />
							<span class="ml10">{{ scope.row.title }}</span>
						</template>
					</template>
				</el-table-column>
				<el-table-column align="center" label="类型" show-overflow-tooltip width="80">
					<template #default="scope">
						<el-tag type="success" size="small">{{ scope.row.type == 1 ? '菜单' : '按钮' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column align="center" label="是否隐藏" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.type != 2">{{ scope.row.isHide ? '隐藏' : '显示' }}</span>
					</template>
				</el-table-column>
				<el-table-column align="center" label="路由名称" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.type != 2">{{ scope.row.name }}</span>
					</template>
				</el-table-column>
				<el-table-column align="center" label="页面缓存" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.type != 2"> {{ scope.row.isKeepAlive ? '是' : '否' }}</span>
					</template>
				</el-table-column>
				<el-table-column align="center" label="排序" show-overflow-tooltip width="80">
					<template #default="scope">
						{{ scope.row.sortNo }}
					</template>
				</el-table-column>

				<el-table-column align="center" label="操作" show-overflow-tooltip width="200">
					<template #default="scope">
						<template v-if="scope.row.level == 1">
							<el-button size="small" text type="primary" @click="onOpenAddMenu(2, scope.row)">新增子菜单</el-button>
							<el-button size="small" text type="primary" @click="onOpenAddMenu(3, scope.row)">编辑</el-button>
						</template>
						<template v-if="scope.row.level == 2">
							<el-button size="small" text type="primary" @click="onOpenAddMenu(3, scope.row)">编辑</el-button>
							<el-button size="small" text type="primary" @click="onOpenAddMenu(4, scope.row)">新增按钮</el-button>
						</template>
						<template v-if="scope.row.level == 3">
							<el-button size="small" text type="primary" @click="onOpenAddMenu(5, scope.row)">编辑</el-button>
						</template>
						<template v-else> </template>
						<el-button size="small" text type="primary" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<MenuDialog ref="menuDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" >
import { defineAsyncComponent, ref, onMounted, reactive } from 'vue';
import { RouteRecordRaw } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';
import { storeToRefs } from 'pinia';
import { useRoutesList } from '/@/stores/routesList';
import { MenuGetPagedList, MenuDeleteMenu, DeleteButton } from '/@/api/menu/index';

// 引入组件
const MenuDialog = defineAsyncComponent(() => import('/@/views/system/menu/dialog.vue'));

// 定义变量内容
const stores = useRoutesList();
const { routesList } = storeToRefs(stores);
const menuDialogRef = ref();
const state = reactive({
	tableData: {
		data: [] as any,
		loading: true,
		menyList: [],
	},
});
// 获取路由数据，真实请从接口获取
const getTableData = async () => {
	try {
		const params = {
			PageIndex: 1,
			PageSize: 9999,
		};
		let data = await MenuGetPagedList(params);
		state.tableData.data = fmtTypeChild(data.data.items);
		console.log(state.tableData.data);
	} finally {
		state.tableData.loading = false;
	}
};
// 遍历格式分类
const fmtTypeChild = (list: any[], level = 1): any[] => {
	return list.map((v: any) => {
		v.level = level;
		if (v.children && v.children.length > 0) {
			v.children = fmtTypeChild(v.children, level + 1);
		} else {
			delete v.children;
		}
		return v;
	});
};
// 打开新增菜单弹窗
const onOpenAddMenu = (type: any, row?: any) => {
	menuDialogRef.value.openDialog(type, row);
};
// 打开编辑菜单弹窗
const onOpenEditMenu = (type: string, row: RouteRecordRaw) => {
	menuDialogRef.value.openDialog(type, row);
};
// 删除当前行
const onTabelRowDel = async (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除${row.type == 1 ? '菜单' : '按钮'}：${row.name}, 是否继续?`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			if (row.type == 1) {
				await MenuDeleteMenu(row.id);
			} else {
				await DeleteButton(row.id);
			}
			ElMessage.success('删除成功');
			getTableData();
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
